<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { parkOverNightCars } from '../../../../api/dashboard/parkOverNightCars'
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    }
  },
  data() {
    return {
      chart: null,
      parkList: [],
      dataName: []
    }
  },
  mounted() {
    parkOverNightCars().then(response => {
      this.parkList = response
      this.forList(this.parkList.slice(0, 5))
      this.initChart(response)
    })
    this.__resizeHanlder = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHanlder)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHanlder)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    forList(parkList) {
      for (var i = 0; i < parkList.length; i++) {
        this.dataName.push(parkList[i].parkname)
      }
    },
    initChart(response) {
      console.log(response)
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['二环东路西百花公园停车场']
        },
        calculable: true,
        series: [
          {
            name: '各停车场隔夜车占比',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: 5, name: '二环东路西百花公园停车场' },
              { value: 3, name: '益寿路北停车场5A04' },
              { value: 38, name: '山大南路南停车场' },
              { value: 29, name: '山大南路北停车场' },
              { value: 11, name: '花园路洪楼南停车场' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
